<?php  if ( ! defined('__CORE_PATH__')) exit('No direct script access allowed');
/* SVN FILE: $Id: app_controller.php 25 2008-08-29 21:58:28Z namaless $ */
/**
 * Cache Class
 *
 * Permette l'uso della cache per la memorizzazione di dati.
 *
 * @author			Pereira Pulido Nuno Ricardo | Namaless | namaless@gmail.com
 * @copyright		Copyright 1981-2008, Namaless.
 * @link			http://www.namaless.com Namaless Blog
 * @version			1.0
 * @version			$Revision: 25 $
 * @modifiedby		$LastChangedBy: namaless $
 * @lastmodified	$Date: 2008-08-29 23:58:28 +0200 (ven, 29 ago 2008) $
 * @license			http://www.opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * Classe Principale.
 */
class Cache
{
    /**
    * Place to store the cache files
    * @var string
    */
    protected static $_path = "cache/";
    
    /**
    * Prefix to use on cache files
    * @var string
    */
    protected static $prefix = "cache_";

	/**
	 * Cache Life.
	 * @var integer
	 */
    protected static $ttl = 900;

    /**
    * Stores data
    * 
    * @param string $group Group to store data under
    * @param string $id    Unique ID of this data
    * @param int    $ttl   How long to cache for (in seconds)
    */
    protected static function write($group, $id, $data)
    {
        $filename = self::get_filename($group, $id);
        
        if ( $fp = fopen($filename, 'w') )
        {
            if ( flock($fp, LOCK_EX) )
            {
                fwrite($fp, $data);
            }
            
            fclose($fp);
            
            // Set filemtime
            touch($filename, time() + self::$ttl);
        }
    }
    
    /**
    * Reads data
    * 
    * @param string $group Group to store data under
    * @param string $id    Unique ID of this data
    */
    protected static function read($group, $id)
    {
        $filename = self::get_filename($group, $id);
        
        return file_get_contents($filename);
    }
    
    /**
    * Determines if an entry is cached
    * 
    * @param string $group Group to store data under
    * @param string $id    Unique ID of this data
    */
    protected static function is_cached($group, $id)
    {
        $filename = self::get_filename($group, $id);

        if ( file_exists($filename) AND @filemtime($filename) > (time() - self::$ttl) )
        {
            return true;
        }

        @unlink($filename);

        return false;
    }
    
    /**
    * Builds a filename/path from group, id and
    * store.
    * 
    * @param string $group Group to store data under
    * @param string $id    Unique ID of this data
    */
    protected static function get_filename($group, $id)
    {
        $id = md5($id);

        return self::$_path . self::$prefix . "{$group}_{$id}";
    }
    
    /**
    * Sets the filename prefix to use
    * 
    * @param string $prefix Filename Prefix to use
    */
    public static function set_prefix($prefix)
    {
        self::$prefix = $prefix;
    }

    /**
    * Sets the store for cache files. Defaults to
    * /dev/shm. Must have trailing slash.
    * 
    * @param string $_path The dir to store the cache data in
    */
    public static function set_store($_path)
    {
        self::$_path = $_path;
    }

    /**
    * Retrieves data from the cache
    * 
    * @param  string $group Group this data belongs to
    * @param  string $id    Unique ID of the data
    * @return mixed         Either the resulting data, or null
    */
    public static function get($group, $id)
    {
        if ( self::is_cached($group, $id) )
        {
            return unserialize(@implode("", self::read($group, $id)));
        }

        return null;
    }
    
    /**
    * Stores data in the cache
    * 
    * @param string $group Group this data belongs to
    * @param string $id    Unique ID of the data
    * @param int    $ttl   How long to cache for (in seconds)
    * @param mixed  $data  The data to store
    */
    public static function put($group, $id, $data)
    {
        self::write($group, $id, serialize($data));
    }

	public static function store($group, $id, $data)
	{
		if ( ! $data_cached = self::get($group, $id) )
		{
			self::put($group, $id, (array)$data);
			return $data;
		}
		else
		{
			return $data_cached;
		}
	}
}




